const { getPool } = require('../../config/db')
const Templates = {
  async getAllTemplates() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM FlexiPage_Templates')
    return rows
  },

  async getTemplateById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM FlexiPage_Templates WHERE template_id = ?', [id])
    return rows[0]
  },

  async createTemplate(templateData) {
    const mysqlPool = await getPool()
    const [result] = await mysqlPool.execute('INSERT INTO FlexiPage_Templates (name, description, css_file_path, js_file_path) VALUES (?, ?, ?, ?)', [templateData.name, templateData.description, templateData.css_file_path, templateData.js_file_path])
    return result.insertId
  },

  async updateTemplate(id, templateData) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('UPDATE FlexiPage_Templates SET name=?, description=?, css_file_path=?, js_file_path=? WHERE template_id=?', [templateData.name, templateData.description, templateData.css_file_path, templateData.js_file_path, id])
  },

  async deleteTemplate(id) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('DELETE FROM FlexiPage_Templates WHERE template_id = ?', [id])
  },
}

module.exports = Templates
